package com.sixth.service.impl;

import com.sixth.annotation.PageInfoAdvice;
import com.sixth.bean.*;
import com.sixth.bean.bo.WXOrderCommentBO;
import com.sixth.bean.common.BasePageInfo;
import com.sixth.bean.vo.WXCommentCountVO;
import com.sixth.bean.vo.WXCommentListVO;
import com.sixth.constant.Constant;
import com.sixth.mapper.MarketCommentMapper;
import com.sixth.mapper.MarketOrderGoodsMapper;
import com.sixth.mapper.MarketOrderMapper;
import com.sixth.mapper.MarketUserMapper;
import com.sixth.service.CommentService;
import com.sixth.util.MarketInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author qys
 * @Description
 * @date 2022/9/9 10:48
 */
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    MarketCommentMapper marketCommentMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Override
    @PageInfoAdvice
    public Object adminCommentList(BasePageInfo basePageInfo, String userId, String valueId) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria marketCommentExampleCriteria = marketCommentExample.createCriteria();
        // controller排除了不是数字的情况，如果参数不是null可以直接转
        if (userId != null && !userId.equals("")) {
            // 不是null就设置条件精准查询
            Integer user = Integer.parseInt(userId);
            marketCommentExampleCriteria.andUserIdEqualTo(user);
        }
        if (valueId != null && !valueId.equals("")) {
            Integer value = Integer.parseInt(valueId);
            marketCommentExampleCriteria.andValueIdEqualTo(value);
        }
        return marketCommentMapper.selectByExample(marketCommentExample);
    }

    @Override
    public int adminCommentDelete(MarketComment marketComment) {
        marketComment.setDeleted(Constant.NOEXIST);
        marketComment.setUpdateTime(new Date());
        marketCommentMapper.updateByPrimaryKeySelective(marketComment);
        return 0;
    }

    @PageInfoAdvice
    @Override
    public Object wxCommentList(Integer valueId, Integer type, BasePageInfo basePageInfo, Integer showType) {
        MarketComment marketComment = new MarketComment();
        // goodsId和topicId
        marketComment.setValueId(valueId);
        marketComment.setType(type.byteValue());
        marketComment.setDeleted(Constant.EXIST);
        if (showType.intValue() == 1) {
            marketComment.setHasPicture(true);
        }
        return marketCommentMapper.selectCommentListAndUserInfoBatched(marketComment);
    }

    @Override
    public WXCommentCountVO wxCommentCount(Integer valueId, Integer type) {
        List<MarketComment> marketComments = getMarketCommentsByValueIdAndType(valueId, type);
        WXCommentCountVO wxCommentCountVO = new WXCommentCountVO();
        if (marketComments.size() == 0) {
            return wxCommentCountVO;
        }
        wxCommentCountVO.setAllCount(marketComments.size());
        long hasPicCount = 0;
        for (MarketComment marketComment : marketComments) {
            if (marketComment.getHasPicture()) {
                hasPicCount++;
            }
        }
        wxCommentCountVO.setHasPicCount(hasPicCount);
        return wxCommentCountVO;
    }

    @Override
    public int wxOrderPost(MarketComment marketComment) {
        // topicId
        Integer topicId = marketComment.getValueId();
        marketComment.setValueId(topicId);
        // String[] picUrls = marketComment.getPicUrls();
        // 不传图片不插入
        // if (picUrls != null && picUrls.length != 0) {
        //     marketComment.setHasPicture(true);
        //     marketComment.setPicUrls(picUrls);
        // }
        marketComment.setUpdateTime(new Date());
        marketComment.setAddTime(new Date());
        // TODO : userId，某个表中信息逻辑删除时修改不成功的响应信息
        Integer userId = MarketInfoUtil.getMarketUser().getId();
        marketComment.setUserId(userId);
        marketCommentMapper.insertSelective(marketComment);
        return 0;
    }

    private List<MarketComment> getMarketCommentsByValueIdAndType(Integer valueId, Integer type) {
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria marketCommentExampleCriteria = marketCommentExample.createCriteria();
        marketCommentExampleCriteria.andValueIdEqualTo(valueId);
        marketCommentExampleCriteria.andTypeEqualTo(type.byteValue());
        marketCommentExampleCriteria.andDeletedEqualTo(Constant.EXIST);
        return marketCommentMapper.selectByExample(marketCommentExample);
    }
}
